package cn.com.dealme.modules;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.nutz.ioc.annotation.InjectName;
import org.nutz.lang.Lang;
import org.nutz.mvc.View;
import org.nutz.mvc.adaptor.JsonAdaptor;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.view.VoidView;
import org.zzm.util.JsonRegexUtils;

import cn.com.dealme.domain.vo.UniversityVO;
import cn.com.dealme.nutz.view.DJsonErrMapView;
import cn.com.dealme.nutz.view.DJsonView;
import cn.com.dealme.service.UniversityService;
import cn.com.dealme.service.exception.UniversityException;


@InjectName("UniversityModule")
@At("/admin/university")
public class UniversityModule {
	
	private UniversityService universityService;

	@At("/list")
	public View list() {
		List<UniversityVO> list = universityService.listAll();
		return new DJsonView(list);
	}
	
	@At("/update")
	@AdaptBy(type=JsonAdaptor.class)
	public View update(@Param("university") UniversityVO university) throws IOException {
		universityService.update(university);
		return DJsonView.createExtSuccessView();
	}
	
	@At("/add")
	@AdaptBy(type=JsonAdaptor.class)
	public View add(@Param("university") UniversityVO university) {
		try {
			universityService.add(university);
		} catch(UniversityException e) {
			return new DJsonErrMapView(e);
		}
		return DJsonView.createExtSuccessView();
	}
	
	@At("/delete")
	@AdaptBy(type=JsonAdaptor.class)
	public View delete(@Param("university") String json) {
		String[] idStrArr = JsonRegexUtils.findAsArray(json);
		Long[] idArr = (Long[]) Lang.array2array(idStrArr, Long.class);
		universityService.delete(idArr);
		return new VoidView();
	}

	public UniversityService getUniversityService() {
		return universityService;
	}

	public void setUniversityService(UniversityService universityService) {
		this.universityService = universityService;
	}


}
